package com.banmao.algorithm.No1To100.No69;

public class Solution1 {

    public static int output(int x) {

        // 对x取半，判断结果在左边还是右边，循环如此
        Long left = 0L;
        Long right = Long.valueOf(Integer.toString(x));
        Long middle = -1L;

        while(left <= right) {
            middle = left + (right - left) / 2;
             System.out.println(middle);
            if(middle * middle > x) {
                // 中间值平方大于原值，结果在左边，收右边界
                right = middle - 1;
            } else if(middle * middle < x) {
                left = middle + 1;
            } else {
                return Integer.parseInt(Long.toString(middle));
            }
        }

        // int lowResult = middle -1;
        // System.out.println("lowResult " + lowResult);

        if(middle * middle > x) {
            return Integer.parseInt(Long.toString(middle - 1));
        }

        return Integer.parseInt(Long.toString(middle));

    }

}
